package sm.upgrade;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.codec.type.Logical;
import sm.upgrade.api.UpgradePhase;
import sm.util.SMMap;

public class MigrateSvcDisplayUpgrade implements UpgradePhase {

	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	@Override
	public void upgrade() {
		logger.info("Migrating data from svcCatalog to svcDisplay...");
		List<Object> svcCatalogs = template.find("from svcCatalog");
		if (svcCatalogs != null) {
			List<String> langArray = new ArrayList<String>();
			List<Object> activeLanguages = template.find(
					"from language where active=?", Logical.TRUE);
			if (activeLanguages != null) {
				for (Object activeLanguage : activeLanguages) {
					SMMap activeLanguageMap = (SMMap) activeLanguage;
					langArray.add((String) activeLanguageMap.get("unique_id"));
				}
			}
			for (Object svcCatalog : svcCatalogs) {
				for (String lang : langArray) {
					SMMap svcCatalogMap = (SMMap) svcCatalog;
					String name = (String) svcCatalogMap.get("name");
					List<Object> svcDisplays = template
							.find("from svcDisplay where name = ? and syslanguage = ?",
									new Object[] { name, lang });

					boolean isNew = true;
					SMMap svcDisplay = new SMMap();
					if (svcDisplays != null && svcDisplays.size() > 0) {
						svcDisplay = (SMMap) svcDisplays.get(0);
						isNew = false;
					}

					svcDisplay.put("name", svcCatalogMap.get("name"));
					svcDisplay.put("displayName", svcCatalogMap.get("name"));
					svcDisplay.put("description",
							svcCatalogMap.get("description"));
					svcDisplay.put("detailedDescription",
							svcCatalogMap.get("detailedDescription"));
					svcDisplay.put("option_instructions",
							svcCatalogMap.get("option_instructions"));
					svcDisplay.put("options", svcCatalogMap.get("options"));
					svcDisplay.put("option_desc",
							svcCatalogMap.get("option_desc"));
					svcDisplay.put("option_names",
							svcCatalogMap.get("option_names"));
					svcDisplay.put("option_names",
							svcCatalogMap.get("option_names"));
					svcDisplay.put("syslanguage", lang);

					if (isNew) {
						template.save("svcDisplay", svcDisplay);
					} else {
						template.update("svcDisplay", svcDisplay);
					}
				}
			}
		}

		String[] oldTriggers = new String[] { "svcCatalog.delete",
				"svcCatalog.set.options.add", "svcCatalog.set.options.update",
				"svcCatalog.set.sort.add", "svcCatalog.set.sort.update",
				"svcCatalog.update" };
		for (String oldTrigger : oldTriggers) {
			Object trigger = template.get("triggers", oldTrigger);
			if (trigger != null) {
				template.delete("triggers", trigger);
				if (logger.isDebugEnabled()) {
					logger.debug("Delete old trigger " + oldTriggers
							+ " in svcCatalog table");
				}
			}
		}

		logger.info("  Finished migrating data from svcCatalog to svcDisplay.");

	}

}
